package com.metadata_system.api_server.system_server;

import com.alibaba.fastjson.JSON;
import com.metadata_system.api.annotations.CheckTokenUser;
import com.metadata_system.common.db.entity.MetadataNode;
import com.metadata_system.common.db.entity.MetadataView;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.http.request_dto.LoginUserRequest;
import com.metadata_system.system.request.MetadataViewListParams;
import com.metadata_system.system.service.MetadataEdgeService;
import com.metadata_system.system.service.MetadataNodeService;
import com.metadata_system.system.service.MetadataService;
import com.metadata_system.system.service.MetadataViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/system-server/v001/MetadataView")
public class MetadataViewController {

    @Autowired
    LoginUserRequest loginUserRequest;

    @Autowired
    MetadataViewService metadataViewService;

    @RequestMapping("getList")
    @CheckTokenUser
    public ApiResult getList(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "metadataId", required = false) Integer metadataId,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "sortProp", required = false) String sortProp,
            @RequestParam(value = "sortOrder", required = false) String sortOrder
    ) {
        return metadataViewService.getList(MetadataViewListParams.builder()
                .name(name)
                .metadataId(metadataId)
                .page(page)
                .pageSize(pageSize)
                .sortProp(sortProp)
                .sortOrder(sortOrder)
                .build());
    }

    @RequestMapping("getDetail")
    @CheckTokenUser
    public ApiResult getDetail(
            @RequestParam(value = "id", required = false) Integer id
    ) {
        return metadataViewService.getDetail(id);
    }

    @RequestMapping("create")
    @CheckTokenUser
    @Transactional
    public ApiResult create(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "shortName", required = false) String shortName,
            @RequestParam(value = "metadataId", required = false) Integer metadataId
    ) {
        ApiResult apiResult = metadataViewService.create(MetadataView.builder()
                .name(name)
                .shortName(shortName)
                .metadataId(metadataId)
                .build(), this.loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }

    @RequestMapping("update")
    @CheckTokenUser
    @Transactional
    public ApiResult update(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "shortName", required = false) String shortName,
            @RequestParam(value = "metadataId", required = false) Integer metadataId
    ) {
        ApiResult apiResult = metadataViewService.update(MetadataView.builder()
                .id(id)
                .name(name)
                .shortName(shortName)
                .metadataId(metadataId)
                .build(), this.loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }

    @RequestMapping("clear")
    @CheckTokenUser
    @Transactional
    public ApiResult clear(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "metadataId", required = false) Integer metadataId
    ) {
        ApiResult apiResult = metadataViewService.clear(id, metadataId,this.loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }

    @RequestMapping("delete")
    @CheckTokenUser
    @Transactional
    public ApiResult delete(
            @RequestParam(value = "id", required = false) Integer id
    ) {
        ApiResult apiResult = metadataViewService.delete(id,this.loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }

    @Autowired
    MetadataEdgeService metadataEdgeService;
    @Autowired
    MetadataNodeService metadataNodeService;
    @Autowired
    MetadataService metadataService;


    @RequestMapping("deleteMoreElement")
    @CheckTokenUser
    @Transactional
    public ApiResult deleteMoreElement(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "nodeIds", required = false) String nodeIds,
            @RequestParam(value = "edgeIds", required = false) String edgeIds
    ) {
        List<Integer> nodeIdList = new ArrayList<>();
        List<Integer> edgeIdList = new ArrayList<>();
        try {
            nodeIdList = JSON.parseArray(nodeIds, Integer.class);
            edgeIdList = JSON.parseArray(edgeIds, Integer.class);
        } catch (Exception e) {
            return ApiResult.error("删除元素-参数错误 1");
        }


        if (!nodeIdList.isEmpty()) {
            ApiResult apiResult = metadataNodeService.deleteAll(nodeIdList, id, this.loginUserRequest.getTokenUser().decodeID());
            if (apiResult.getCode() != 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return apiResult;
            }

        }
        if (!edgeIdList.isEmpty()) {
            ApiResult apiResult = metadataEdgeService.deleteAll(edgeIdList, id, this.loginUserRequest.getTokenUser().decodeID());
            if (apiResult.getCode() != 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return apiResult;
            }
        }

        return metadataService.getDetailByVewId(id);
    }
}
